package pos.dato;

import java.sql.*;
import java.util.LinkedList;
import java.util.List;
import pos.dominio.Etiqueta;


public class JDBCEtiquetaDAO implements IEtiquetaDAO{

    public void insert( Connection conn, String etiquetaID) {
    	
    	String titulo = null;
        PreparedStatement stmt = null;
        String sql = "INSERT INTO Etiquetas ( etiquetaID, titulo) VALUES (?, ?) ";

        try {
    	    stmt = conn.prepareStatement(sql);
        
            stmt.setString(1, etiquetaID);
            stmt.setString(2, titulo);
            
            stmt.executeUpdate();
            
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
    }


	public List<Etiqueta> selectAllEtiquetas() {
		Connection conn = ConnectionManager.getInstance().checkOut();

        PreparedStatement stmt = null;
        List<Etiqueta> searchResults = new LinkedList<Etiqueta>();
        ResultSet result = null;

        try {

            String sql = "SELECT * FROM Etiquetas";
            stmt = conn.prepareStatement(sql); 
            stmt.executeQuery();
            result = stmt.executeQuery();
            while (result.next()) {
                Etiqueta temp = new Etiqueta();
                temp.setEtiquetaID(result.getString("etiquetaID"));
                temp.setTitulo(result.getString("titulo"));
                
                searchResults.add(temp);
            }
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            ConnectionManager.getInstance().checkIn(conn);
            try {
                if (result != null)
                    result.close();
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }

        }
        return searchResults;
	}
	

	public Etiqueta select(Connection conn, String etiquetaID) {
        PreparedStatement stmt = null;
        Etiqueta temp = new Etiqueta();
        ResultSet result = null;
        String sql = "SELECT * FROM Etiquetas WHERE (etiquetaID = ?)";

        try {   
            stmt = conn.prepareStatement(sql); 
            stmt.setString(1, etiquetaID);
            result = stmt.executeQuery();           
            result.next();
            temp.setEtiquetaID(result.getString("etiquetaID"));
            temp.setTitulo(result.getString("titulo"));   
            
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (result != null)
                    result.close();
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {}

        }
        return temp;
	}
}

	
